<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:20%;height: 60%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="模板">
          <span><a href="./doc/stock_check_template.xls" target="_blank"><b>Excel模板下载</b></a></span>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="仓库">
          <a-select placeholder="请选择仓库" v-decorator.trim="[ 'depotId' ]" :dropdownMatchSelectWidth="false" allow-clear>
            <a-select-option v-for="(item,index) in depotList" :key="index" :value="item.id">
              {{ item.depotName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="文件">
          <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader"
                   :data="setFileData" :action="importExcelUrl" @change="handleImportExcel">
            <a-button type="primary" icon="import">导入</a-button>
          </a-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {getAction} from '@/api/manage'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import Vue from 'vue'
  export default {
    name: "StockCheckImportModal",
    components: {
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        depotList: [],
        tokenHeader: {'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          importExcelUrl: "/api/plugin/stock-check/stockCheck/importStockCheckExcel",
        }
      }
    },
    created () {
      this.getDepotList()
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}${this.url.importExcelUrl}`;
      }
    },
    methods: {
      add () {
        this.form.resetFields();
        this.model = Object.assign({}, {});
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      handleOk() {

      },
      getDepotList() {
        let that = this;
        getAction('/depot/findDepotByCurrentUser').then((res) => {
          if(res.code === 200){
            that.depotList = res.data
          }
        })
      },
      /* 导入 */
      handleImportExcel(info){
        if (info.file.status !== 'uploading') {
          console.log(info.file, info.fileList);
        }
        if (info.file.status === 'done') {
          if (info.file.response) {
            // this.$message.success(`${info.file.name} 文件上传成功`);
            if (info.file.response.code === 200) {
              this.$message.success('文件导入成功')
              this.$emit('ok', info.file.response.data.rows);
              this.close()
            } else {
              this.$message.warn(info.file.response.data)
            }
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.data}.`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`文件导入失败: ${info.file.msg} `);
        }
      },
      setFileData() {
        let obj = {}
        let depotId= this.form.getFieldValue('depotId')
        if(depotId) {
          obj = {
            'depotId': depotId
          }
        }
        return obj
      }
    }
  }
</script>
<style scoped>

</style>